import './MyPromise'

const getNumber = () => {
  return Math.round(Math.random() * 10)
}

const promiseDemo = () => {
  const p = new Promise((resolve, reject) => {
    if (getNumber() % 2 === 0) {
      resolve('success')
    } else {
      reject('reject')
    }
  })

  p.then(
    res => {
      console.log(res, '1 res then')
      return new Promise((resolve, reject) => {
        getNumber() % 2 === 0 ? resolve('suc') : reject('fail')
      })
    },
    err => {
      console.log(err, '1 err then')
      return Promise.reject('fail')
    }
  )
    .then(
      res => {
        console.log(res, '2 res then')
      },
      err => console.log(err, '2 err then')
    )
    .finally(() => {
      console.log(p, 'p')
      p.then(
        res => console.log(res, 'final res then'),
        err => console.log(err, 'final err then')
      )
    })
}

const myPromiseDemo1 = () => {
  const p = new MyPromise((resolve, reject) => {
    setTimeout(() => {
      if (getNumber() % 2 === 0) {
        resolve('success')
        // reject('reject')
      } else {
        reject('reject')
      }
    }, 500)
  })
  p.then(
    res => {
      console.log(res, '1 res then')
    },
    err => {
      console.log(err, '1 err then')
    }
  )
  setTimeout(() => {
    p.then(
      res => {
        console.log(res, '2 res then')
        return new MyPromise((r, e) => e('then resolve'))
      },
      err => {
        console.log(err, '2 err then')
        return 'hhhh error'
      }
    )
      .then(
        res => {
          console.log(res, '3 res then')
        },
        err => {
          console.log(err, '3 err then')
        }
      )
      .then(
        res => {
          console.log(res, '4 res then')
        },
        err => {
          console.log(err, '4 err then')
        }
      )
  }, 400)
}

// myPromiseDemo1()

const promiseDemo1 = () => {
  const p = new Promise((resolve, reject) => {
    setTimeout(() => {
      if (getNumber() % 2 === 0) {
        resolve('success')
        // reject('reject')
      } else {
        reject('reject')
      }
    }, 500)
  })
  p.then(
    res => {
      console.log(res, '1 res then')
    },
    err => {
      console.log(err, '1 err then')
    }
  )
  setTimeout(() => {
    p.then(
      res => {
        console.log(res, '2 res then')
        return new Promise((r, e) => e('then resolve'))
      },
      err => {
        console.log(err, '2 err then')
        return 'hhhh error'
      }
    )
      .then(
        res => {
          console.log(res, '3 res then')
        },
        err => {
          console.log(err, '3 err then')
        }
      )
      .then(
        res => {
          console.log(res, '4 res then')
        },
        err => {
          console.log(err, '4 err then')
        }
      )
  }, 400)
}

// promiseDemo1()

{
  const promiseDemo2 = () => {
    const p1 = new Promise(resolve => resolve('p1'))
    const p2 = new Promise(resolve => resolve('p2'))
    const p3 = new Promise(resolve => resolve('p3'))
    const p4 = new Promise((resolve, reject) => {
      if (getNumber() % 2 === 0) {
        resolve('p4')
      } else {
        reject('p4')
      }
    })
    const p5 = new Promise(resolve => resolve('p5'))
    const p6 = new Promise((resolve, reject) => {
      if (getNumber() % 2 === 0) {
        resolve('p6')
      } else {
        reject('p6')
      }
    })
    Promise.all([p1, p2, p3, p4, p5, p6]).then(
      res => {
        console.log(res, 'all res')
      },
      err => {
        console.log(err, 'all err')
      }
    )
  }
  const myPromiseDemo2 = () => {
    const p1 = new MyPromise(resolve => resolve('p1'))
    const p2 = new MyPromise(resolve => resolve('p2'))
    const p3 = new MyPromise(resolve => resolve('p3'))
    const p4 = new MyPromise((resolve, reject) => {
      if (getNumber() % 2 === 0) {
        resolve('p4')
      } else {
        reject('p4')
      }
    })
    const p5 = new MyPromise(resolve => resolve('p5'))
    const p6 = new MyPromise((resolve, reject) => {
      if (getNumber() % 2 === 0) {
        resolve('p6')
      } else {
        reject('p6')
      }
    })
    MyPromise.all([p1, p2, p3, p4, p5, p6]).then(
      res => {
        console.log(res, 'my all res')
      },
      err => {
        console.log(err, 'my all err')
      }
    )
  }
  // promiseDemo2()
  // myPromiseDemo2()
}

// Demo3
{
  const promiseDemo3 = () => {
    const p1 = new Promise(resolve => {
      setTimeout(() => {
        resolve('p1')
      }, 500)
    })
    const p2 = new Promise(resolve => {
      setTimeout(() => {
        resolve('p2')
      }, 700)
    })
    const p3 = new Promise((r, e) => {
      const time = getNumber() % 2 === 0 ? 80 : 800
      setTimeout(() => {
        e('p3')
      }, time)
    })
    Promise.race([p1, p2, p3]).then(
      res => console.log(res, 'race res'),
      err => console.log(err, 'race err')
    )
  }

  const myPromiseDemo3 = () => {
    const p1 = new MyPromise(resolve => {
      setTimeout(() => {
        resolve('p1')
      }, 500)
    })
    const p2 = new MyPromise(resolve => {
      setTimeout(() => {
        resolve('p2')
      }, 700)
    })
    const p3 = new MyPromise((r, e) => {
      const time = getNumber() % 2 === 0 ? 80 : 800
      setTimeout(() => {
        e('p3')
      }, time)
    })
    MyPromise.race([p1, p2, p3]).then(
      res => console.log(res, 'my race res'),
      err => console.log(err, 'my race err')
    )
  }

  // promiseDemo3()
  myPromiseDemo3()
}
